package com.wxq.koland.xiaoliscan.network;

import com.wxq.koland.xiaoliscan.bean.NormalBean;
import com.wxq.koland.xiaoliscan.utils.LogUtil;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.Response;

/**
 * 数据返回统一处理
 * @param <T>
 */
public abstract class BaseObserver<T> implements Observer<Response<NormalBean<T>>> {
    private static final String TAG = "BaseObserver";
    @Override
    public void onNext(Response<NormalBean<T>> tResponse) {
        LogUtil.e(TAG,"code:==="+tResponse.code());
        LogUtil.e(TAG,"errorBody===:"+tResponse.errorBody());
        LogUtil.e(TAG,"Body===:"+tResponse.body());
        if(tResponse.code()==404){
            onFailure(new Exception("404"),"页面不存在");
            return;
        }
        NormalBean<T> body = tResponse.body();
        if(body.getResultCode()==1){
            onSuccess(body.getOter());
        }else{
            onFailure(new Exception(body.getResultMessage()), body.getResultMessage());
        }
    }

    @Override
    public void onError(Throwable e) {
        onFailure(e,RxExceptionUtil.exceptionHandler(e));
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onSubscribe(Disposable d) {

    }

    public abstract void onSuccess(T result);

    public abstract void onFailure(Throwable e,String errorMsg);

}